#include <stdio.h>
#include "magicScripts.h"
#include <stdlib.h>


//tac tac toe newborn
void attack(void);
void decide_winner(void);
void defend(void);
void pc_move(void);
void usr_move(void);
void gatherInfo(void);
void print_current_config(void);
int usrPntsCnt=0,pcPntsCnt=0;

typedef struct Points{
    int row;
    int col;
} POINT; 

POINT pcPntArry[10],usrPntArray[10];
int pcPntsCnt,usrPntsCnt;


char tic_tac_toe_mat[3][3] = {'\0'};

void gatherInfo()
{
    pcPntsCnt = usrPntsCnt = 0;
    
    for (int i=0; i<3; i++)
        for (int j=0; j<3; j++)
        {
            if(tic_tac_toe_mat[i][j] == 'X')
            {
                usrPntArray[usrPntsCnt].row = i;
                usrPntArray[usrPntsCnt++].col = j;
                
            }
              else if (tic_tac_toe_mat[i][j] == 'O')
            {
                pcPntArry[pcPntsCnt].row = i;
                pcPntArry[pcPntsCnt++].col = j;           
                    
            }
        }

}

void pc_move()
{
    
    switch(usrPntsCnt)
    {
        case 0 : tic_tac_toe_mat [rand()%2] [rand()%2] = 'O';
            break;
            
        case 1 :
        { POINT p = usrPntArray[usrPntsCnt-1];
            
            if(p.row+1<=2)
            tic_tac_toe_mat [p.row+1] [p.col] = 'O';
            else if(p.row-1>=0)
                tic_tac_toe_mat [p.row-1] [p.col] = 'O';
            else if(p.col+1>=0)
                tic_tac_toe_mat [p.row] [p.col+1] = 'O';
            else if(p.row-1>=0)
                tic_tac_toe_mat [p.row] [p.col-1] = 'O';
            
            break;
        }
        case 2 : defend();
            break;
            
        case 3 : 
        {
            POINT p1 = pcPntArry[pcPntsCnt-1];
            POINT p2 = pcPntArry[pcPntsCnt-2];
            POINT p3;
            if(p1.row == p2.row || p1.col == p2.col)
            {
                if(p1.row == p1.col && p2.row == p2.col)
                {
                    p3.row = 3- (p1.row + p2.row); 
                    p3.col = 3- (p1.col + p2.col);
                }
                else if(p1.col ==  p2.col)
                {
                    p3.col = p1.col;
                    p3.row = 3- (p1.row + p2.row);
                }
                else if(p1.row == p2.row){
                    p3.row = p1.row;
                    p3.col = 3- (p1.col + p2.col);
                    
                }
                
                 tic_tac_toe_mat[p3.row][p3.col] = 'O';
            }
        
        }
            decide_winner();
            break;
        
    } 

}

void attack()
{
    POINT p = pcPntArry[0];
    
    for (int i=0; i<2; i++)
        for (int j=0; j<2; j++)
        {
          if((i==0 && j==0) || p.row>2 || p.col>2 || p.row<0 || p.col<0)
              continue;
          else {
              
              if(tic_tac_toe_mat[p.row+i][p.col+j] == '\0' )
              {
                  tic_tac_toe_mat[p.row+i][p.col+j] = 'O';
                  return;
              }
              if(tic_tac_toe_mat[p.row-i][p.col-j] == '\0')
              {
                  tic_tac_toe_mat[p.row-i][p.col-j] = 'O';
                  return;
              }
              if(tic_tac_toe_mat[p.row+i][p.col-j] == '\0')
              {
                  tic_tac_toe_mat[p.row+i][p.col-j] = 'O';
                  return;
              }
              if(tic_tac_toe_mat[p.row-i][p.col+j] == '\0')
              {
                  tic_tac_toe_mat[p.row-i][p.col+j] = 'O';
                  return;
              }
              
          }
        }
  
}
void defend()
{
    POINT p1 = usrPntArray[0];
    POINT p2 = usrPntArray[1];
    POINT p3;
    if(p1.row == p1.col && p2.row == p2.col)
    {
        p3.row = 3- (p1.row + p2.row); 
        p3.col = 3- (p1.col + p2.col);
    }
    else if(p1.col ==  p2.col)
    {
        p3.col = p1.col;
        p3.row = 3- (p1.row + p2.row);
    }
    else if(p1.row == p2.row){
        p3.row = p1.row;
        p3.col = 3- (p1.col + p2.col);
        
    }else {
        attack();
        return;
    }
    
    tic_tac_toe_mat[p3.row][p3.col] = 'O';
}

//
//void pc_move()
//{
//    POINT p3;
//    if(!isUnTouched(p1) && !isUnTouched(p2))
//    {
//      if(p1.row == p1.col && p2.row == p2.col)
//      {
//          p3.row = 3- (p1.row + p2.row); 
//          p3.col = 3- (p1.col + p2.col);
//      }
//        else if(p1.col ==  p2.col)
//        {
//            p3.col = p1.col;
//            p3.row = 3- (p1.row + p2.row);
//        }
//        else if(p1.row == p2.row){
//            p3.row = p1.row;
//            p3.col = 3- (p1.col + p2.col);
//
//        }
//        
//        tic_tac_toe_mat[p3.row][p3.col] = 'O';
//    }
//    else 
//    {for (int index = 0; index<3; index++) {
//        for (int index2 = 0; index2<3; index2++) {
//            if(tic_tac_toe_mat[index][index2] == '\0')
//            {    tic_tac_toe_mat[index][index2]= 'O';
//                return;
//            }
//        }
//    }
//        
//    }
//}

void restart(void)
{
    for (int index = 0; index<3; index++) {
        for (int index2 = 0; index2<3; index2++) {
  
                tic_tac_toe_mat[index][index2]= '\0';
        }
        }

    main();
       
}
void decide_winner()
{
    if(usrPntsCnt <3)
        return;
    
    POINT p1,p2,p3;
    
    p1 = usrPntArray[0];
    p2 = usrPntArray[1];
    p3 = usrPntArray[2];
    if((p1.col+p2.col+p3.col ==3 && p1.row+p2.row+p3.row ==3))
    {  print_current_config();
        printf("USR...:)");
        restart();
    exit(0);
        
    }

    p1 = pcPntArry[0];
     p2 = pcPntArry[1];
     p3 = pcPntArry[2];
    if(p1.col+p2.col+p3.col ==3 && p1.row+p2.row+p3.row ==3)
    {   print_current_config();
        printf("PC...:)");
        restart();
    exit(0);
    }
    
          
}

_Bool isValidLoc(int row, int col)
{
    
           if(tic_tac_toe_mat[row][col] == '\0')
                return 1;
            else {
                return 0;
            }
  
}
void usr_move()
{
    int row,col;
    do {
        
        printf("\nEnetr Your Move Location : as (row<3 col<3) :");
        scanf("%d %d", &row,&col);
        
        
    } while (row >=3 || col >= 3);
    if(row == -9 || col == -9)
        restart();
    if(isValidLoc(row,col))
    tic_tac_toe_mat[row][col] ='X';
    else {
        usr_move();
    }
//        usrPntsCnt++;
}

void print_current_config()
{
    for (int i=0; i<3 ; i++)
    {
        printf("\n %c | %c | %c " , tic_tac_toe_mat[i][0]!='\0'?tic_tac_toe_mat[i][0]:' '
               ,tic_tac_toe_mat[i][1]!='\0'?tic_tac_toe_mat[i][1]:' '
               ,tic_tac_toe_mat[i][2]!='\0'?tic_tac_toe_mat[i][2]:' ');
        i==2?0:printf("\n----------- ");
    }
    
}

void playTicTacToe(void)
{
    for (int index = 0; index<3; index++) {
        for (int index2 = 0; index2<3; index2++) {
            
            tic_tac_toe_mat[index][index2]= '\0';
        }
    }
    
    for(;;)
    {
        print_current_config();
        usr_move();
        gatherInfo();
        pc_move();
        gatherInfo();
        decide_winner();
    }  
    
    
}
   